/*
 * Copyright 2007 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ycy.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.ycy.ss.status.AllStatus;
import com.ycy.ss.status.StatusSina;

import weibo4j.Oauth;
import weibo4j.Timeline;
import weibo4j.Weibo;
import weibo4j.http.AccessToken;
import weibo4j.model.Status;
import weibo4j.model.StatusWapper;
import weibo4j.model.WeiboException;
import weibo4j.util.BareBonesBrowserLaunch;

/**
 * A trivial consumer of the 'friends_timeline' service at Tumblr.
 * 
 * @author John Kristian
 */
public class SinaConsumer extends HttpServlet{
	private Logger logger = LogManager.getLogger(SinaConsumer.class);

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		if ("1".equals(request.getParameter("clear"))) {
			AllStatus.sina = null;
			response.getWriter().write("clear");
			return;
		}

		String code = request.getParameter("code");
		if (code == null || code.length() == 0) {
			Oauth oauth = new Oauth();
			try {
//				BareBonesBrowserLaunch.openURL(oauth.authorize("code"));
				response.sendRedirect(oauth.authorize("code"));
			} catch (WeiboException e) {
				logger.warn("", e);
				response.getWriter().write("failed");
				return;
			}
		} else {
			StatusSina statusSina = new StatusSina();
			Oauth oauth = new Oauth();
			try {
				AccessToken accessToken = oauth.getAccessTokenByCode(code);
				statusSina.ACCESS_TOKEN = accessToken;

				Weibo weibo = new Weibo();
				weibo.setToken(accessToken.getAccessToken());
				Timeline tm = new Timeline();

				// Paging paging = new Paging();
				// paging.setCount(1);

				StatusWapper status = tm.getUserTimeline();
				List<Status> statusList = new ArrayList<Status>();
				for (Status s : status.getStatuses()) {
					statusList.add(s);
				}

				if (statusList != null && statusList.size() != 0) {
					statusSina.readOk(statusList.get(0).getId());
				} else {
					statusSina.readOk("0");
				}
			} catch (WeiboException e) {
				logger.warn("", e);
				response.getWriter().write("failed");
				return;
			}

			AllStatus.sina = statusSina;
			response.getWriter().write("ok");

		}
	}

}
